#ifndef _URISTREAMOPENER_HPP_
#define _URISTREAMOPENER_HPP_

#include "jhdefs.hpp"
#include "event/mutex.hpp"
using JHCPP::event::CMutex;
#include "filesys/path.hpp"
using JHCPP::filesys::CPath;
#include <istream>
#include <map>

NET_NAMESPACE_BEGIN

class CURI;
class CURIStreamFactory;

class CURIStreamOpener
{
public:
	enum
	{
		MAX_REDIRECTS = 10
	};

	CURIStreamOpener();
	~CURIStreamOpener();

	std::istream* open(const CURI& uri) const;
	std::istream* open(const std::string& pathOrURI) const;
	std::istream* open(const std::string& basePathOrURI, const std::string& pathOrURI) const;

	void registerStreamFactory(const std::string& scheme, CURIStreamFactory* pFactory);
	void unregisterStreamFactory(const std::string& scheme);
	bool supportsScheme(const std::string& scheme);
	static CURIStreamOpener& defaultOpener();

protected:
	std::istream* openFile(const CPath& path) const;
	std::istream* openURI(const std::string& scheme, const CURI& uri) const;

private:
	CURIStreamOpener(const CURIStreamOpener&);
	CURIStreamOpener& operator = (const CURIStreamOpener&);

	typedef std::map<std::string, CURIStreamFactory*> FactoryMap;

	FactoryMap		m_map;
	mutable CMutex	m_mutex;
};


NET_NAMESPACE_END

#endif
